using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using UnityEngine.UI;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_MapQuestItem : UIBaseNode
    {
        #region Template Generate,don't modify

        protected class UIB_UIN_MapQuestItem
        {
            #region ObjectBinding Generate

            public Toggle tglQuest { protected set; get; }
            public ExText text { protected set; get; }
            public ExImage questBg { protected set; get; }

            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<Toggle>("tglQuest", out var __tbv0);
                tglQuest = __tbv0;
                __binding.TryGetVariableValue<ExText>("text", out var __tbv1);
                text = __tbv1;
                __binding.TryGetVariableValue<ExImage>("questBg", out var __tbv2);
                questBg = __tbv2;
            }

            #endregion ObjectBinding Generate
        }

        #endregion Template Generate,don't modify

        #region fields

        private int _questCid;

        private int _entityId;
        #endregion fields
        public Toggle toggle => ui.tglQuest;
        #region properties

        protected UIB_UIN_MapQuestItem ui { get; set; }

        #endregion properties

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_MapQuestItem();
            ui.InitBinding(csObjBind);
        }

        protected override void OnInit()
        {
            ui.tglQuest.onValueChanged.AddListener(OnValueChanged);
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {

        }

        protected override void OnDispose()
        {
            ui.tglQuest.onValueChanged.RemoveAllListeners();
        }

        #region methonds

        public void Refresh(int entityId, int questCid)
        {
            _questCid = questCid;
            _entityId = entityId;
            ui.text.text = TableCenter.quest.Get(questCid).Name;
        }

        private void OnValueChanged(bool isOn)
        {
            if (!isOn || belongWindow is not UILevelMap uiMap) return;
            var questPanel = uiMap.GetPanel<UIN_LevelMapQuestPanel>();
            uiMap.PushPanel(questPanel);
            questPanel.Refresh(_questCid, _entityId);
        }

        #endregion methonds
    }
}